import { defineStore } from 'pinia'
import { useRequest } from "@usehook"
import { baseUrl } from "@/utils/constant"

const useMainStore = defineStore("main", {
    state: () => ({
        email: "",
        icpNumber: "",
        gaNumber: "",
        websiteName: "",
        name: "",
        captcha: false,
        allowUserPwd: false,
        expireTime: 60,
        domain: "",
        forceRefresh: false,
        menu: [],
        urlMap: {},
        paths: [],
        apiInfo: {},
        defaultSource: {}
    }),
    getters: {
        getMenu: state => state.menu,
        getPaths: state => state.paths,
        getUrlMap: state => state.urlMap,
        getApiInfo: state => state.apiInfo,
        getWebInfo: state => ({
            email: state.email,
            icpNumber: state.icpNumber,
            gaNumber: state.gaNumber,
            websiteName: state.websiteName,
            name: state.name,
            captcha: state.captcha,
            allowUserPwd: state.allowUserPwd,
            expireTime: state.expireTime,
            domain: state.domain
        }),
        getForceRefresh() {
            const refresh = this.forceRefresh
            nextTick(() => {
                this.forceRefresh = false
            })
            return refresh
        }
    },
    actions: {
        setWebInfo(info) {
            this.$patch((state) => {
                state.email = info.email
                state.icpNumber = info.icpNumber
                state.gaNumber = info.gaNumber
                state.websiteName = info.websiteName
                state.domain = info.domain
                state.name = info.name
                state.captcha = info.captcha
                state.allowUserPwd = info.allowUserPwd
                state.expireTime = info.expireTime || 60
            })
        },
        setForceRefresh() {
            this.forceRefresh = true
        },
        async setApiInfo() {
            let success = true

            if (!this.defaultSource || Object.keys(this.defaultSource).length === 0) {
                const apiSource = await useRequest(baseUrl + "/swagger-resources")
                this.defaultSource = apiSource[0]
            }
            const apiInfo = await useRequest(baseUrl + this.defaultSource.location)
            this.paths = apiInfo.paths
            this.apiInfo = apiInfo.info
            this.apiInfo.server = this.defaultSource
            this.apiInfo.components = apiInfo.components
            const urlMap = {}
            const menu = apiInfo.tags.map(e => { return { name: e.name || e.description, children: [], status: 1, icon: "Folder" } })
            for (let url of Object.keys(apiInfo.paths)) {
                const methods = apiInfo.paths[url]
                for (let method of Object.keys(methods)) {
                    const methodInfo = methods[method]
                    const tags = methodInfo.tags
                    for (let tag of tags) {
                        const index = menu.findIndex(m => m.name === tag)
                        if (index !== -1) {
                            urlMap["/" + menu[index].name + "/" + methodInfo.operationId] = { ...methodInfo, url, method }
                            menu[index].children.push({ name: methodInfo.summary, status: 1, method: method, url: "/" + menu[index].name + "/" + methodInfo.operationId, icon: "Document" })
                        }
                        if (menu[index]) menu[index].num = (menu[index]?.num || 0) + 1
                    }
                }
            }
            menu.unshift({ name: "资料库", status: 1, icon: "DocumentCopy", children: [{ name: "模型组件", status: 1, icon: "", url: "/model" }] })
            if (apiInfo.commonResponse && Object.keys(apiInfo.commonResponse).length > 0) {
                urlMap["/commonResponse"] = apiInfo.commonResponse
                menu[0].children.push({ name: "通用响应参数", status: 1, icon: "Document", url: "/commonResponse" })
            }
            menu.unshift({ name: "首页", status: 1, icon: "HomeFilled", url: "/" })
            this.menu = menu
            this.urlMap = urlMap
            return success
        },
    },
    persist: {
        enabled: true,
        key: "webInfo",
        storage: localStorage,
        paths: ["websiteName"]
    }
})

export default useMainStore